|
This section deals with advanced access modifiers, such as internal, aggregation and namespaces and touches on modules and assemblies. Lets start off by quickly looking at the final access modifier, the internal keyword. This is access modifier makes the code accessible at the component or assembly level. Internal classes, methods and properties are similar to the friendship concept in C++ and Visual Basic. Internal access allows the code to be accessed from any other code compiled within the same assembly. // Test
Assembly: testAssembly.cs
// compile with /target:library public class
testClass public static int Test() Test Application: // Test
Program
// Compile with /reference:testAssembly.dll class Program // Will not compile as test application 1
is not a member // OK to use a getter
method. Internal methods cannot be accessed from outside the assembly they are compiled to, but you can use a getter and setter to modify the data. Internal access prevents a private field from being overwritten to allow direct access to the data. AggregationSmall objects are not really too useful on their own, but when lots of small objects come together and form a much larger object, then you start to see the power of object orientated programming. Aggregation is the name given to this grouping of objects. Think of a Windows form. The form by itself isn't really much use to anybody, nor is a button, input box or drop down list, but when these objects are combined together they form an object that is useful. The power of object orientation is in the relationships between the objects, not in the individual objects themselves. Objects are built from other objects to form object collaborations, or aggregates. This is where the internal access modifier is most used. Public objects have unlimited access, and you may wish to secure this data, but private access does not allow the objects to interact. Protected cannot be used as the objects are not inheriting from other objects. The internal modifier allows a private-like access level to all objects within the collaboration. You can also have a protected internal access modifier, which, as you may think, allows access to the class from the current assembly or from types derived from the containing class. Aggregation is not the same a inheritance. Aggregation and inheritance both provide ways to create larger classes from smaller classes, but they do this in completely different ways. Aggregation is a relationship between objects, whereas inheritance is a relationship between classes. There is no special coding syntax for aggregation; it is just the way that objects are constructed. In this example, the combination of the wheel, engine and brakes objects form the aggregation of the car object. class Wheel
{ int Diameter; } class Engine class Brakes class Car
Tutorials in this Series
Leave a ReplyClick here to cancel reply.
|
|







